package com.times.server.rabbitmq.consumer;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;

/**
 * @version V1.0.0
 * @Title: 基本消息模型-消费者
 * @Package com.times.server.rabbitmq.consumer
 * @Description: ${TODO}
 * @author: TimesPanda
 * @date: 2020-03-31 02:04
 * @Copyright: 2020-03 Inc. All rights reserved.
 * 注意：本内容仅限于公司内部传阅，禁止外泄以及用于其他的商业目
 */
public class BasicConsumer {
    //定义日志
    private static final Logger log = LoggerFactory.getLogger(BasicConsumer.class);
    //定义Json序列化和反序列化实例
    @Autowired
    public ObjectMapper objectMapper;

    /**
     * 监听并接收消费队列中的消息-在这里采用单一容器工厂实例即可
     */
    @RabbitListener(queues = "{mq.basic.info.queue.name}", containerFactory="singleListenerContainer")
    //由于消息本质上是一串二进制数据流,因而监听接收的消息采用字节数组接收
    public void consumeMsg(@Payload byte[] msg) {
        try {
            //将字节数组的消息转化为字符串并打印
            String message = new String(msg, "utf-8");
            log.info("基本消息模型-消费者-监听消费到消息：{}", message);
        } catch (Exception e) {
            log.error("基本消息模型-消费者-发生异常：", e.fillInStackTrace());
        }
    }
}
